﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Snowdreamist.Collection;

namespace Snowdreamist.Runtime.Services
{
    public class ModuleMonitor : IModuleMonitor, IModuleAware
    {
        public ModuleMonitor()
        {
            this.Modules = new ReadonlyConcurrentObservableCollection<Tuple<IModule, IModuleDefinition>>(this._modules);
        }

        public virtual void Initialize() { }

        public ReadonlyConcurrentObservableCollection<Tuple<IModule, IModuleDefinition>> Modules { get; private set; }

        public void OnModuleLoaded(IModuleDefinition definition, IModule module)
        {
            this._modules.Add(new Tuple<IModule, IModuleDefinition>(module, definition));
        }

        public void OnModuleDestroying(IModuleDefinition definition, IModule module)
        {
            this._modules.Remove(new Tuple<IModule, IModuleDefinition>(module, definition));
        }

        public virtual void OnRuntimeDisposing()
        {
            this._modules.Dispose();
        }

        #region private

        private readonly ConcurrentObservableCollection<Tuple<IModule, IModuleDefinition>> _modules = new ConcurrentObservableCollection<Tuple<IModule, IModuleDefinition>>();

        #endregion private
    }
}